import java.io.*;
import java.util.*;

/**
 * Encapsulates the main entry point.
 */
public class Main
{
    /**
     * The main entry-point of the application.
     */
    public static void main(String args[])
    {
        if (args == null || args.length != 2) {
            System.out.println("usage: java Main <query> <config>");
            return;
        }

        String queryFile = args[0];
        String configFile = args[1];

        File file1 = new File(queryFile);
        if (!file1.exists()) {
            System.out.format("The query file %s could not be found.\n", queryFile);
            return;
        }
        
        File file2 = new File(configFile);
        if (!file2.exists()) {
            System.out.format("The config file %s could not be found.\n", configFile);
            return;
        }

        try {
            EstimatedCosts costs = new EstimatedCosts(configFile);
            FileInputStream fstream = new FileInputStream(queryFile);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String query;
            while ((query = br.readLine()) != null) {
                // Instantiate our optimizer for the specific query and estimated costs.
                QueryOptimizer optimizer = new QueryOptimizer(query, costs);

                // Calculate the optimal plan.
                optimizer.compute();

                // Print the results.
                optimizer.printResults();
            }

            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}